/**    
 * @{#} UploadZhengshuActivity.java Create on 2014-7-10 下午7:55:24    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.o.app.media.PickPhotoTask;
import cn.o.app.media.PickPhotoTask.PickPhotoListener;
import cn.o.app.media.TakePhotoTask;
import cn.o.app.media.TakePhotoTask.TakePhotoListener;
import cn.o.app.ui.OActionSheet;
import cn.o.app.ui.OActionSheet.OActionItem;
import cn.o.app.ui.OActionSheet.OnActionItemClickListener;
import cn.skinapp.R;

import com.evan.common.adapter.BaseAdapter;
import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.lidroid.xutils.BitmapUtils;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class UploadZhengshuActivity extends BasicActivity {

	private String key;
	JSONArray array;

	private ListView list_zhengshu;
	private ZhengshuAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_upload_zhengshu);

		init();

		String title = getIntent().getStringExtra(Constant.IDENTITY_KEY_TITLE);
		setNavTitle(title);
		setNavRightBtn("上传");

		key = getIntent().getStringExtra(Constant.IDENTITY_KEY);
		try {
			if (key.equals("Certificate")) {
				array = new JSONArray(
						JsonUtil.convert(GlobalSettings.sUser.Certificate));
			} else if (key.equals("Employee_Card")) {
				array = new JSONArray(
						JsonUtil.convert(GlobalSettings.sUser.Employee_Card));
			} else if (key.equals("IdCard")) {
				array = new JSONArray(
						JsonUtil.convert(GlobalSettings.sUser.IdCard));
			} else {
				array = new JSONArray();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		list_zhengshu = (ListView) findViewById(R.id.list_zhengshu);
		adapter = new ZhengshuAdapter(this, array, key);
		list_zhengshu.setAdapter(adapter);
	}

	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btn_left) {
			finish();
		} else if (id == R.id.text_right) {
			ArrayList<OActionItem> dataProvider = new ArrayList<OActionSheet.OActionItem>();
			OActionItem item = null;
			item = new OActionItem();
			item.setText("拍照");
			dataProvider.add(item);
			item = new OActionItem();
			item.setText("从手机相册选择");
			dataProvider.add(item);
			OActionSheet<OActionItem> actionSheet = new OActionSheet<OActionSheet.OActionItem>(
					this);
			actionSheet.setCancel("取消");
			actionSheet.setDataProvider(dataProvider);
			actionSheet.show(false, true);
			actionSheet
					.setOnActionItemClickListener(new OnActionItemClickListener<OActionSheet.OActionItem>() {

						@Override
						public void onItemClick(
								OActionSheet<OActionItem> sheet, View v,
								int position, OActionItem dataItem) {
							if (position == 0) {
								TakePhotoTask task = new TakePhotoTask(
										UploadZhengshuActivity.this,
										GlobalSettings.REQUEST_CODE_TAKE_PHOTO);
								task.takePhoto();
								task.setListener(new TakePhotoListener() {

									@Override
									public void onComplete(Uri uri) {
										uploadZhengshu(uri.getPath());
									}
								});
							}
							if (position == 1) {
								PickPhotoTask task = new PickPhotoTask(
										UploadZhengshuActivity.this,
										GlobalSettings.REQUEST_CODE_PICK_PHOTO);
								task.pickPhoto();
								task.setListener(new PickPhotoListener() {

									@Override
									public void onComplete(Uri uri) {
										uploadZhengshu(uri.getPath());
									}
								});
							}
						}
					});
		}
	}

	protected void uploadZhengshu(String path) {
		
		
		Log.i("testerror", "path:" + path);
		
		
		File f = new File(path);

		Log.i("testerror", "file.exist:" + (f.exists()));
		
		
		
		if (f.exists()) {
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
			params.put("pic", f);
			params.put(Constant.PARAM_API, "SYS.UploadPic");
			RequestTaskIntercept.requestIntercept(getContext(),
					Constant.URL.REQUEST, new RequestHandler(getContext(),
							new HandlerCallback() {

								@Override
								public void callback(Object o) {
									if (!CommonUtility.isNull(o)) {
										try {
											JSONObject object = (JSONObject) o;
											if (CommonUtility
													.response200(object)) {
												final String path = object
														.getString(Constant.JSON_PARAM_RES);
												HashMap<String, Object> params_ = new HashMap<String, Object>();
												params_.put(
														Constant.PARAM_TOKEN,
														GlobalSettings.sToken);
												params_.put(key, path);
												params_.put(Constant.PARAM_API,
														"User.Set");
												RequestTaskIntercept
														.requestIntercept(
																getContext(),
																Constant.URL.REQUEST,
																new RequestHandler(
																		getContext(),
																		new HandlerCallback() {
																			@Override
																			public void callback(
																					Object o) {
																				if (!CommonUtility
																						.isNull(o)) {
																					JSONObject object = (JSONObject) o;
																					if (CommonUtility
																							.response200(object)) {
																						try {
																							GlobalSettings.sUser = JsonUtil
																									.convertFromObject(
																											object.getJSONObject("Res"),
																											User.class);
																							if (key.equals("Certificate")) {
																								array = new JSONArray(
																										JsonUtil.convert(GlobalSettings.sUser.Certificate));
																							} else if (key
																									.equals("Employee_Card")) {
																								array = new JSONArray(
																										JsonUtil.convert(GlobalSettings.sUser.Employee_Card));
																							} else if (key
																									.equals("IdCard")) {
																								array = new JSONArray(
																										JsonUtil.convert(GlobalSettings.sUser.IdCard));
																							} else {
																								array = new JSONArray();
																							}
																							adapter.notifyUpdate(array);
																						} catch (Exception e) {
																							e.printStackTrace();
																						}
																					}
																				}
																			}
																		}),
																params_);
											}
										} catch (Exception e) {
											e.printStackTrace();
										}
									}

								}
							}), params);
		}
	}

	public static class ZhengshuAdapter extends BaseAdapter implements
			OnClickListener {

		private String param;

		protected BitmapUtils mBitmapUtils;

		public ZhengshuAdapter(Activity activity, JSONArray array) {
			super(activity, array);
			mBitmapUtils = new BitmapUtils(activity);
		}

		public ZhengshuAdapter(Activity activity, JSONArray array, String param) {
			this(activity, array);
			this.param = param;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (CommonUtility.isNull(convertView)) {
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.item_zhengshu, null);
				viewHolder.img_icon = (ImageView) convertView
						.findViewById(R.id.img_icon);
				CommonUtility.setViewHolderTag(convertView, viewHolder);
			} else {
				viewHolder = (ViewHolder) CommonUtility
						.getViewHolderTag(convertView);
			}
			String pic_path = (String) getItem(position);
			TextView text_delete_zhengzhuang = (TextView) convertView
					.findViewById(R.id.text_delete_zhengzhuang);
			text_delete_zhengzhuang.setOnClickListener(this);

			mBitmapUtils
					.display(viewHolder.img_icon, GlobalSettings.SERVER_IMG_URL
							+ pic_path + Constant.IMG_SRC);

			return convertView;
		}

		@Override
		public Object getItem(int position) {
			try {
				return array.getString(position);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		class ViewHolder {
			ImageView img_icon;
		}

		@Override
		public void onClick(View view) {
			HashMap<String, Object> params_ = new HashMap<String, Object>();
			params_.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
			params_.put(param, "");
			params_.put(Constant.PARAM_API, "User.Set");
			RequestTaskIntercept.requestIntercept(activity,
					Constant.URL.REQUEST, new RequestHandler(activity,
							new HandlerCallback() {

								@Override
								public void callback(Object o) {
									if (!CommonUtility.isNull(o)) {
										JSONObject object = (JSONObject) o;
										if (CommonUtility.response200(object)) {
											try {
												GlobalSettings.sUser = JsonUtil.convertFromObject(
														object.getJSONObject("Res"),
														User.class);
												array = object.getJSONObject(
														"Res").getJSONArray(
														param);

												notifyUpdate(array);
											} catch (Exception e) {
												e.printStackTrace();
											}
										}
									}
								}
							}), params_);
		}
	}
	
	
	/*@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
